package com.wu.shop.module.shop.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.CompoundButton
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.wkq.lib_base.adapter.KtAdapter
import com.wu.common.base.adapter.DataBindingViewHolder
import com.wu.shop.R
import com.wu.shop.databinding.ItemShopCarBinding
import com.wu.shop.databinding.ItemShopCarGoodsBinding
import com.wu.shop.module.shop.model.GoodsInfo
import com.wu.shop.module.shop.model.ShopCarInfo
import com.wu.shop.observable.ShopCarBuyObservable
import com.wu.shop.observable.shopcar.ShopCarBuyInfo
import com.wu.shop.util.ShopCarUtil

/**
 *
 * 作者:吴奎庆
 *
 * 时间:2021/7/6
 *
 * 用途:
 */


class ShopCarGoodsInfoAdapter(mContext: Context) : KtAdapter<GoodsInfo>(mContext) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        var viewDatabinding = DataBindingUtil.inflate<ItemShopCarGoodsBinding>(
            LayoutInflater.from(mContext),
            R.layout.item_shop_car_goods,
            parent,
            false
        )
        var bindingHolder = DataBindingViewHolder(viewDatabinding.root)
        bindingHolder.binding = viewDatabinding
        return bindingHolder
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        var bindingHolder = holder as DataBindingViewHolder
        var binding = bindingHolder.binding as ItemShopCarGoodsBinding
        binding!!.tvGoodsName.text = getItem(position)!!.goodsName
        binding!!.tvMoney.text = getItem(position)!!.goodsPrice
        binding!!.tvNum.text = getItem(position)!!.goodsNum.toString()
        Glide.with(mContext).applyDefaultRequestOptions(RequestOptions().centerCrop().dontAnimate().dontTransform().diskCacheStrategy(
            DiskCacheStrategy.AUTOMATIC)).load(getItem(position)!!.goodsIcon).into(binding!!.ivGoodsIcon)
        if (getItem(position)!!.isChecked) {
            binding.checkbox.setBackgroundResource(R.drawable.iv_shop_select)
        } else {
            binding.checkbox.setBackgroundResource(R.drawable.iv_shop_defult)
        }

        binding!!.checkbox.setOnClickListener {
            getItem(position)!!.isChecked= !getItem(position)!!.isChecked
            if (getItem(position)!!.isChecked) {
                binding.checkbox.setBackgroundResource(R.drawable.iv_shop_select)
            } else {
                binding.checkbox.setBackgroundResource(R.drawable.iv_shop_defult)
            }
            if (getItem(position)!!.isChecked) {
                ShopCarUtil.processAddOrSub(
                    getItem(position)!!.goodsId,
                    getItem(position)!!.goodsPrice,
                    true,
                    getItem(position)!!.goodsNum,
                    getItem(position)!!.shopId
                )
            } else {
                ShopCarUtil.processAddOrSub(
                    getItem(position)!!.goodsId,
                    getItem(position)!!.goodsPrice,
                    false,
                    getItem(position)!!.goodsNum,
                    getItem(position)!!.shopId
                )
            }
        }
    }

}